package me.bcloud.web.servlet;

import java.io.IOException;
import java.io.OutputStream;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import me.bcloud.web.domain.JobConfig;
import me.bcloud.web.domain.OnlineCounter;
import me.bcloud.web.spring.Cache;
import me.bcloud.web.service.JobConfigService;
import org.springframework.stereotype.Controller;

@Controller("job")
public class JobServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Resource
	private JobConfigService jobConfigService;

	public void setJobConfigService(JobConfigService jobConfigService) {
		this.jobConfigService = jobConfigService;
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		OutputStream out = resp.getOutputStream();

		//在线人数统计
		OnlineCounter.resie(req.getRemoteAddr());
		// 获取任务 如果无任务发送NO_JOB字符
		JobConfig jobConfig = jobConfigService.getJob();
		if (jobConfig != null) {
			
			req.getSession().setAttribute("job", jobConfig.getId());
			if (jobConfig.getTestUrl()!=null && !jobConfig.getTestUrl().equals("")) {
				out.write((jobConfig.getPname()+ "|" + jobConfig.getTestUrl() + "|" + jobConfig.getTestStr()).getBytes());
			}else {
				out.write(jobConfig.getPname().getBytes());
			}
		}
		else {
			out.write("NO_JOB".getBytes());
		}
		

	}


}
